package poketrade;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import poketrade.entities.News;
import poketrade.entities.User;
import poketrade.managers.NewsManager;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet
public class NewsServlet extends BaseHttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private NewsManager mgr;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<News> news = mgr.getNews();
		request.setAttribute("list", news);
		forward(request, response, "/accueil.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		
		
		if (((User) request.getSession(true).getAttribute("user")).getAdmin()){
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			if (title.length()>0 && content.length()>0)	{
				mgr.createNews(title,content);
			}
		}
		response.sendRedirect("/Poketrade/NewsServlet");	
	}

}
